<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>5.1 数据库 | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c05/s02.html" />
    
    
    <link rel="prev" href="../c05/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.1"
        data-chapter-title="5.1 数据库"
        data-filepath="c05/s01.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="51-&#x6570;&#x636E;&#x5E93;">5.1 &#x6570;&#x636E;&#x5E93;</h1>
<p>&#x4E0E;Django&#x6846;&#x67B6;&#x76F8;&#x6BD4;&#xFF0C;Tornado&#x6CA1;&#x6709;&#x81EA;&#x5E26;ORM&#xFF0C;&#x5BF9;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x53BB;&#x9002;&#x914D;&#x3002;&#x6211;&#x4EEC;&#x4F7F;&#x7528;MySQL&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<p>&#x5728;Tornado3.0&#x7248;&#x672C;&#x4EE5;&#x524D;&#x63D0;&#x4F9B;tornado.database&#x6A21;&#x5757;&#x7528;&#x6765;&#x64CD;&#x4F5C;MySQL&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x800C;&#x4ECE;3.0&#x7248;&#x672C;&#x5F00;&#x59CB;&#xFF0C;&#x6B64;&#x6A21;&#x5757;&#x5C31;&#x88AB;&#x72EC;&#x7ACB;&#x51FA;&#x6765;&#xFF0C;&#x4F5C;&#x4E3A;torndb&#x5305;&#x5355;&#x72EC;&#x63D0;&#x4F9B;&#x3002;torndb&#x53EA;&#x662F;&#x5BF9;MySQLdb&#x7684;&#x7B80;&#x5355;&#x5C01;&#x88C5;&#xFF0C;&#x4E0D;&#x652F;&#x6301;Python 3&#x3002;</p>
<h2 id="torndb&#x5B89;&#x88C5;">torndb&#x5B89;&#x88C5;</h2>
<pre><code class="lang-python">pip install torndb
</code></pre>
<h2 id="&#x8FDE;&#x63A5;&#x521D;&#x59CB;&#x5316;">&#x8FDE;&#x63A5;&#x521D;&#x59CB;&#x5316;</h2>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x5E94;&#x7528;&#x542F;&#x52A8;&#x65F6;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x5B9E;&#x4F8B;&#xFF0C;&#x4F9B;&#x5404;&#x4E2A;RequestHandler&#x4F7F;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x6784;&#x9020;Application&#x7684;&#x65F6;&#x5019;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x5B9E;&#x4F8B;&#x5E76;&#x4F5C;&#x4E3A;&#x5176;&#x5C5E;&#x6027;&#xFF0C;&#x800C;RequestHandler&#x53EF;&#x4EE5;&#x901A;&#x8FC7;self.application&#x83B7;&#x53D6;&#x5176;&#x5C5E;&#x6027;&#xFF0C;&#x8FDB;&#x800C;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> torndb

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span><span class="hljs-params">(tornado.web.Application)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        handlers = [
            (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), <span class="hljs-string">&quot;templates&quot;</span>),
            static_path=os.path.join(os.path.dirname(__file__), <span class="hljs-string">&quot;statics&quot;</span>),
            debug=<span class="hljs-keyword">True</span>,
        )
        super(Application, self).__init__(handlers, **settings)
        <span class="hljs-comment"># &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5168;&#x5C40;mysql&#x8FDE;&#x63A5;&#x5B9E;&#x4F8B;&#x4F9B;handler&#x4F7F;&#x7528;</span>
        self.db = torndb.Connection(
            host=<span class="hljs-string">&quot;127.0.0.1&quot;</span>,
            database=<span class="hljs-string">&quot;itcast&quot;</span>,
            user=<span class="hljs-string">&quot;root&quot;</span>,
            password=<span class="hljs-string">&quot;mysql&quot;</span>
        )
</code></pre>
<h2 id="&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;">&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;</h2>
<p>&#x65B0;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x4E0E;&#x8868;&#xFF1A;</p>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">database</span> <span class="hljs-string">`itcast`</span> <span class="hljs-keyword">default</span> <span class="hljs-built_in">character</span> <span class="hljs-keyword">set</span> utf8;</span>

<span class="hljs-operator"><span class="hljs-keyword">use</span> itcast;</span>

<span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> houses (
    <span class="hljs-keyword">id</span> <span class="hljs-built_in">bigint</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment <span class="hljs-keyword">comment</span> <span class="hljs-string">&apos;&#x623F;&#x5C4B;&#x7F16;&#x53F7;&apos;</span>,
    title <span class="hljs-built_in">varchar</span>(<span class="hljs-number">64</span>) <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">default</span> <span class="hljs-string">&apos;&apos;</span> <span class="hljs-keyword">comment</span> <span class="hljs-string">&apos;&#x6807;&#x9898;&apos;</span>,
    <span class="hljs-keyword">position</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">32</span>) <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">default</span> <span class="hljs-string">&apos;&apos;</span> <span class="hljs-keyword">comment</span> <span class="hljs-string">&apos;&#x4F4D;&#x7F6E;&apos;</span>,
    price <span class="hljs-built_in">int</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>,
    score <span class="hljs-built_in">int</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">default</span> <span class="hljs-number">5</span>,
    comments <span class="hljs-built_in">int</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>,
    primary <span class="hljs-keyword">key</span>(<span class="hljs-keyword">id</span>)
)<span class="hljs-keyword">ENGINE</span>=<span class="hljs-keyword">InnoDB</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">charset</span>=utf8 <span class="hljs-keyword">comment</span>=<span class="hljs-string">&apos;&#x623F;&#x5C4B;&#x4FE1;&#x606F;&#x8868;&apos;</span>;</span>
</code></pre>
<h3 id="1-&#x6267;&#x884C;&#x8BED;&#x53E5;">1. &#x6267;&#x884C;&#x8BED;&#x53E5;</h3>
<ul>
<li>execute(query, <em>parameters, *</em>kwparameters) &#x8FD4;&#x56DE;&#x5F71;&#x54CD;&#x7684;&#x6700;&#x540E;&#x4E00;&#x6761;&#x81EA;&#x589E;&#x5B57;&#x6BB5;&#x503C;</li>
<li>execute_rowcount(query, <em>parameters, *</em>kwparameters) &#x8FD4;&#x56DE;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;</li>
</ul>
<p>query&#x4E3A;&#x8981;&#x6267;&#x884C;&#x7684;sql&#x8BED;&#x53E5;&#xFF0C;parameters&#x4E0E;kwparameters&#x4E3A;&#x8981;&#x7ED1;&#x5B9A;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python">db.execute(<span class="hljs-string">&quot;insert into houses(title, position, price, score, comments) values(%s, %s, %s, %s, %s)&quot;</span>, <span class="hljs-string">&quot;&#x72EC;&#x7ACB;&#x88C5;&#x4FEE;&#x5C0F;&#x522B;&#x5885;&quot;</span>, <span class="hljs-string">&quot;&#x7D27;&#x90BB;&#x6587;&#x6D25;&#x8857;&quot;</span>, <span class="hljs-number">280</span>, <span class="hljs-number">5</span>, <span class="hljs-number">128</span>)
&#x6216;
db.execute(<span class="hljs-string">&quot;insert into houses(title, position, price, score, comments) values(%(title)s, %(position)s, %(price)s, %(score)s, %(comments)s)&quot;</span>, title=<span class="hljs-string">&quot;&#x72EC;&#x7ACB;&#x88C5;&#x4FEE;&#x5C0F;&#x522B;&#x5885;&quot;</span>, position=<span class="hljs-string">&quot;&#x7D27;&#x90BB;&#x6587;&#x6D25;&#x8857;&quot;</span>, price=<span class="hljs-number">280</span>, score=<span class="hljs-number">5</span>, comments=<span class="hljs-number">128</span>)
</code></pre>
<p>&#x6267;&#x884C;&#x8BED;&#x53E5;&#x4E3B;&#x8981;&#x7528;&#x6765;&#x6267;&#x884C;&#x975E;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InsertHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        title = self.get_argument(<span class="hljs-string">&quot;title&quot;</span>)
        position = self.get_argument(<span class="hljs-string">&quot;position&quot;</span>)
        price = self.get_argument(<span class="hljs-string">&quot;price&quot;</span>)
        score = self.get_argument(<span class="hljs-string">&quot;score&quot;</span>)
        comments = self.get_argument(<span class="hljs-string">&quot;comments&quot;</span>)
        <span class="hljs-keyword">try</span>:
            ret = self.application.db.execute(<span class="hljs-string">&quot;insert into houses(title, position, price, score, comments) values(%s, %s, %s, %s, %s)&quot;</span>, title, position, price, score, comments)
        <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
            self.write(<span class="hljs-string">&quot;DB error:%s&quot;</span> % e)
        <span class="hljs-keyword">else</span>:
            self.write(<span class="hljs-string">&quot;OK %d&quot;</span> % ret)
</code></pre>
<h3 id="2-&#x67E5;&#x8BE2;&#x8BED;&#x53E5;">2. &#x67E5;&#x8BE2;&#x8BED;&#x53E5;</h3>
<ul>
<li>get(query, <em>parameters, *</em>kwparameters) &#x8FD4;&#x56DE;&#x5355;&#x884C;&#x7ED3;&#x679C;&#x6216;None&#xFF0C;&#x82E5;&#x51FA;&#x73B0;&#x591A;&#x884C;&#x5219;&#x62A5;&#x9519;&#x3002;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;torndb.Row&#x7C7B;&#x578B;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x5B57;&#x5178;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5373;&#x540C;&#x65F6;&#x652F;&#x6301;&#x5B57;&#x5178;&#x7684;&#x5173;&#x952E;&#x5B57;&#x7D22;&#x5F15;&#x548C;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x76F8;&#x8BBF;&#x95EE;&#x3002;</li>
<li>query(query, <em>parameters, *</em>kwparameters) &#x8FD4;&#x56DE;&#x591A;&#x884C;&#x7ED3;&#x679C;&#xFF0C;torndb.Row&#x7684;&#x5217;&#x8868;&#x3002;</li>
</ul>
<p>&#x4EE5;&#x4E0A;&#x4E00;&#x7AE0;&#x8282;&#x6A21;&#x677F;&#x4E2D;&#x7684;&#x6848;&#x4F8B;&#x6765;&#x6F14;&#x793A;&#xFF0C;&#x5148;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;index.html&#x6A21;&#x677F;&#xFF0C;&#x5C06;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">&quot;house-title&quot;</span>&gt;</span>{{title_join(house[&quot;titles&quot;])}}<span class="hljs-tag">&lt;/<span class="hljs-title">span</span>&gt;</span>
</code></pre>
<p>&#x6539;&#x4E3A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">span</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">&quot;house-title&quot;</span>&gt;</span>{{house[&quot;title&quot;]}}<span class="hljs-tag">&lt;/<span class="hljs-title">span</span>&gt;</span>
</code></pre>
<p>&#x6DFB;&#x52A0;&#x4E24;&#x4E2A;&#x65B0;&#x7684;handler&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GetHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#x4E3A;http://127.0.0.1/get?id=111&quot;&quot;&quot;</span>
        hid = self.get_argument(<span class="hljs-string">&quot;id&quot;</span>)
        <span class="hljs-keyword">try</span>:
            ret = self.application.db.get(<span class="hljs-string">&quot;select title,position,price,score,comments from houses where id=%s&quot;</span>, hid)
        <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
            self.write(<span class="hljs-string">&quot;DB error:%s&quot;</span> % e)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">print</span> type(ret)
            <span class="hljs-keyword">print</span> ret
            <span class="hljs-keyword">print</span> ret.title
            <span class="hljs-keyword">print</span> ret[<span class="hljs-string">&apos;title&apos;</span>]
            self.render(<span class="hljs-string">&quot;index.html&quot;</span>, houses=[ret])


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">QueryHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x8BBF;&#x95EE;&#x65B9;&#x5F0F;&#x4E3A;http://127.0.0.1/query&quot;&quot;&quot;</span>
        <span class="hljs-keyword">try</span>:
            ret = self.application.db.query(<span class="hljs-string">&quot;select title,position,price,score,comments from houses limit 10&quot;</span>)
        <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
            self.write(<span class="hljs-string">&quot;DB error:%s&quot;</span> % e)
        <span class="hljs-keyword">else</span>:
            self.render(<span class="hljs-string">&quot;index.html&quot;</span>, houses=ret)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c05/index.html" class="navigation navigation-prev " aria-label="Previous page: 5 数据库"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c05/s02.html" class="navigation navigation-next " aria-label="Next page: 5.2 练习"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
